package cn.znk.kedis.master_slave_model.aspect;

import cn.znk.kedis.master_slave_model.template.KedisTemplate;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import javax.annotation.Resource;

/**
 * IntelliJ IDEA
 * project: kedis-spring-boot-starter
 *
 * @author shimi
 * @since 2021/11/22
 * @version 1.0.2
 */
@Aspect
public class KedisDynamicAspect {

    @Resource
    private KedisTemplate kedisTemplate;

    @Pointcut("execution(* cn.znk.kedis.master_slave_model.template.KedisTemplate.getCache(..))")
    public void readValuePointcut(){}

    @Pointcut("execution(* cn.znk.kedis.master_slave_model.template.KedisTemplate.setCache(..))")
    public void writeValuePointcut1(){}

    @Pointcut("execution(* cn.znk.kedis.master_slave_model.template.KedisTemplate.delCache(..))")
    public void writeValuePointcut2(){}

    @Before("readValuePointcut())")
    public void setRead() {
        kedisTemplate.setDynamic(kedisTemplate.getR());
    }

    @Before("writeValuePointcut1() || writeValuePointcut2())")
    public void setWrite() {
        kedisTemplate.setDynamic(kedisTemplate.getW());
    }

}
